package cn.t.repository.impl.user;

import cn.t.converter.user.RoleResourceConverter;
import cn.t.core.repoistory.RepositoryImpl;
import cn.t.model.user.RoleResourceDO;
import cn.t.persistence.entity.user.RoleResourcePO;
import cn.t.persistence.mapper.user.RoleResourceMapper;
import cn.t.repository.user.RoleResourceRepository;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 部门信息表数据仓库
 * @author l
 */
@Slf4j
@Repository
@AllArgsConstructor
public class RoleResourceRepositoryImpl extends RepositoryImpl<RoleResourceMapper, RoleResourcePO, RoleResourceConverter, RoleResourceDO> implements RoleResourceRepository {


    @Override
    public List<RoleResourceDO> findByModel(RoleResourceDO roleResourceDO, int start, Integer pageSize, String field, String direction) {

        String lastSql = StringUtils.hasText(field) ? " order by " + field + " " + direction + " limit " + start + "," + pageSize : " limit " + start + "," + pageSize;

        List<RoleResourcePO> list = getWrapper(roleResourceDO).last(lastSql).list();
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }

        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }

    private LambdaQueryChainWrapper<RoleResourcePO> getWrapper(RoleResourceDO roleResourceDO) {
        LambdaQueryChainWrapper<RoleResourcePO> wrapper = lambdaQuery();
        if (!ObjectUtils.isEmpty(roleResourceDO.getGmtCreatedStart()) && !ObjectUtils.isEmpty(roleResourceDO.getGmtCreatedEnd())) {
            wrapper.between(RoleResourcePO::getGmtCreated, roleResourceDO.getGmtCreatedStart(), roleResourceDO.getGmtCreatedEnd());
        }
        if (!ObjectUtils.isEmpty(roleResourceDO.getGmtModifiedStart()) && !ObjectUtils.isEmpty(roleResourceDO.getGmtModifiedEnd())) {
            wrapper.between(RoleResourcePO::getGmtModified, roleResourceDO.getGmtModifiedStart(), roleResourceDO.getGmtModifiedEnd());
        }
        if (!ObjectUtils.isEmpty(roleResourceDO.getResourceId())) {
            wrapper.eq(RoleResourcePO::getResourceId, roleResourceDO.getResourceId());
        }
        if (!ObjectUtils.isEmpty(roleResourceDO.getRoleId())) {
            wrapper.eq(RoleResourcePO::getRoleId, roleResourceDO.getRoleId());
        }

        if (!ObjectUtils.isEmpty(roleResourceDO.getStatus())) {
            wrapper.eq(RoleResourcePO::getStatus, roleResourceDO.getStatus());
        }
        return wrapper;
    }

    @Override
    public long count(RoleResourceDO roleResourceDO) {
        return getWrapper(roleResourceDO).count();
    }

    @Override
    public boolean removeByRoleId(Long roleId) {
        return getBaseMapper().removeByRoleId(roleId);
    }

    @Override
    public boolean removeByResourceId(Long resourceId) {
        return getBaseMapper().removeByRoleId(resourceId);
    }
}
